package com.scoy.honeymei.calendar;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.scoy.honeymei.R;
import com.scoy.honeymei.bean.DateBean;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * @author nanchen
 * @fileName CalendarViewDemo
 * @packageName com.nanchen.calendarview
 * @date 2016/12/08  10:40
 */

public class CalendarAdapter extends BaseAdapter {
    private boolean isLeapyear = false; // 是否为闰年
    private int daysOfMonth = 0; // 某月的天数
    private int dayOfWeek = 0; // 具体某一天是星期几
    private int lastDaysOfMonth = 0; // 上一个月的总天数
    private Context context;
    private String[] dayNumber = new String[42]; // 一个gridview中的日期存入此数组中
    private SpecialCalendar sc = null;
    private Resources res = null;
    private String currentYear = "";
    private String currentMonth = "";
    private String currentDay = "";

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-dd");
    private String showYear = ""; // 用于在头部显示的年份
    private String showMonth = ""; // 用于在头部显示的月份
    private String animalsYear = "";
    private String leapMonth = ""; // 闰哪一个月
    private String cyclical = ""; // 天干地支
    // 系统当前时间
    private String sysDate = "";
    private String sys_year = "";
    private String sys_month = "";
    private String sys_day = "";
    private int colorDataPosition = -1;

    private boolean isClickData = false;
    private ArrayList<DateBean> datalist;
    private ArrayList<String> dataquan;

    private static final String TAG = "CalendarAdapter";


    public CalendarAdapter(Context context, ArrayList<DateBean> datalist) {
        this.context = context;
        this.datalist = datalist;
    }

    public CalendarAdapter() {
        Date date = new Date();
        sysDate = sdf.format(date); // 当期日期
        sys_year = sysDate.split("-")[0];
        sys_month = sysDate.split("-")[1];
        sys_day = sysDate.split("-")[2];

    }

    public CalendarAdapter(Context context, Resources rs, int jumpMonth,
                           int jumpYear, int year_c, int month_c, int day_c, ArrayList<DateBean> datalist) {
        this();
        this.context = context;
        sc = new SpecialCalendar();
        this.res = rs;
        this.datalist = datalist;

        dataquan = new ArrayList<>();

        int stepYear = year_c + jumpYear;
        int stepMonth = month_c + jumpMonth;

        if (stepMonth > 0) {
            // 往下一个月滑动
            if (stepMonth % 12 == 0) {
                stepYear = year_c + stepMonth / 12 - 1;
                stepMonth = 12;
            } else {
                stepYear = year_c + stepMonth / 12;
                stepMonth = stepMonth % 12;
            }
        } else {
            // 往上一个月滑动
            stepYear = year_c - 1 + stepMonth / 12;
            stepMonth = stepMonth % 12 + 12;
            if (stepMonth % 12 == 0) {

            }
        }

        currentYear = String.valueOf(stepYear); // 得到当前的年份
        currentMonth = String.valueOf(stepMonth); // 得到本月
        // （jumpMonth为滑动的次数，每滑动一次就增加一月或减一月）
        currentDay = String.valueOf(day_c); // 得到当前日期是哪天

        getCalendar(Integer.parseInt(currentYear),
                Integer.parseInt(currentMonth));

    }

    public CalendarAdapter(Context context, Resources rs, int year, int month,
                           int day) {
        this();
        this.context = context;
        sc = new SpecialCalendar();
        this.res = rs;
        currentYear = String.valueOf(year);// 得到跳转到的年份
        currentMonth = String.valueOf(month); // 得到跳转到的月份
        currentDay = String.valueOf(day); // 得到跳转到的天
        getCalendar(Integer.parseInt(currentYear),
                Integer.parseInt(currentMonth));
    }

    @Override
    public int getCount() {
        return dayNumber.length;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.calen_calendar_item, null);
        }

        TextView num_tv = convertView.findViewById(R.id.num_tv);
        ImageView dot_iv = convertView.findViewById(R.id.dot_iv);
        TextView price_tv = convertView.findViewById(R.id.price_tv);

        String d = dayNumber[position];
        num_tv.setText(d);
        num_tv.setTextColor(Color.parseColor("#00333333"));

        if (position < daysOfMonth + dayOfWeek && position >= dayOfWeek) {
            // 当前月信息显示
            num_tv.setTextColor(Color.BLACK);// 当月字体设黑

            if (colorDataPosition == position) {
                num_tv.setTextColor(Color.WHITE);
                num_tv.setBackgroundResource(R.drawable.bg_circle);
            } else {
                num_tv.setBackgroundColor(Color.WHITE);
                num_tv.setTextColor(Color.BLACK);
            }

            String strPos = num_tv.getText().toString().length() == 1 ? "0" + num_tv.getText().toString() : num_tv.getText().toString();

            String showmonth = getShowMonth().length() == 1 ? "0" + getShowMonth() : getShowMonth();

            if (strPos.equals(sys_day) && showmonth.equals(sys_month) && getShowYear().equals(sys_year)) {
                num_tv.setText("今");
            }

            String showYear = getShowYear();

            String cDay = showYear + "-" + showmonth + "-" + strPos;


            for (DateBean dbe : datalist) {
                String date0 = dbe.getDate();
                if (cDay.equals(date0)) {
                    price_tv.setVisibility(View.VISIBLE);
                    price_tv.setText("￥" + dbe.getPrice());
                    break;
                }
            }

        }
        return convertView;
    }

    // 得到某年的某月的天数且这月的第一天是星期几
    private void getCalendar(int year, int month) {
        isLeapyear = sc.isLeapYear(year); // 是否为闰年
        daysOfMonth = sc.getDaysOfMonth(isLeapyear, month); // 某月的总天数
        dayOfWeek = sc.getWeekdayOfMonth(year, month); // 某月第一天为星期几
        lastDaysOfMonth = sc.getDaysOfMonth(isLeapyear, month - 1); // 上一个月的总天数
        getweek(year, month);
    }

    // 将一个月中的每一天的值添加入数组dayNuber中
    private void getweek(int year, int month) {
        int j = 1;
        // 得到当前月的所有日程日期(这些日期需要标记)
        for (int i = 0; i < dayNumber.length; i++) {
            if (i < dayOfWeek) { // 前一个月
                int temp = lastDaysOfMonth - dayOfWeek + 1;
                dayNumber[i] = temp + i + "";
            } else if (i < daysOfMonth + dayOfWeek) { // 本月
                String day = String.valueOf(i - dayOfWeek + 1); // 得到的日期
                dayNumber[i] = i - dayOfWeek + 1 + "";

                // 对于当前月才去标记当前日期
                if (sys_year.equals(String.valueOf(year))
                        && sys_month.equals(String.valueOf(month))
                        && sys_day.equals(day)) {
                    // 标记当前日期
                    colorDataPosition = i;
                }

                //当月
                if (datalist != null) {
                   /* for (int m = 0; m < datalist.size(); m++) {
                        DateBean dateBean = datalist.get(m);
                        String myyear = dateBean.getDate().substring(0, 4);
                        String mymonth = dateBean.getDate().substring(5, 7);
                        String myday = dateBean.getDate().substring(8, 10);
                        if (myyear.equals(String.valueOf(year))
                                && Integer.parseInt(mymonth) == month
                                && Integer.parseInt(myday) == Integer.parseInt(day)) {
                            // 标记当前日期
                            dataquan.add(myday);
                        }
                    }*/
                }

                setShowYear(String.valueOf(year));
                setShowMonth(String.valueOf(month));

            } else { // 下一个月
                dayNumber[i] = j + "";
                j++;
            }
        }

        String abc = "";
        for (int i = 0; i < dayNumber.length; i++) {
            abc = abc + dayNumber[i] + ":";
        }
        Log.d("DAYNUMBER", abc);

    }

    public void matchScheduleDate(int year, int month, int day) {

    }

    /**
     * 点击每一个item时返回item中的日期
     *
     * @param position
     * @return
     */
    public String getDateByClickItem(int position) {
        return dayNumber[position];
    }

    /**
     * 在点击gridView时，得到这个月中第一天的位置
     *
     * @return
     */
    public int getStartPositon() {
        return dayOfWeek + 7;
    }

    /**
     * 在点击gridView时，得到这个月中最后一天的位置
     *
     * @return
     */
    public int getEndPosition() {
        return (dayOfWeek + daysOfMonth + 7) - 1;
    }

    public String getShowYear() {
        return showYear;
    }

    public void setShowYear(String showYear) {
        this.showYear = showYear;
    }

    public String getShowMonth() {
        return showMonth;
    }

    public void setShowMonth(String showMonth) {
        this.showMonth = showMonth;
    }

    public String getAnimalsYear() {
        return animalsYear;
    }

    public void setAnimalsYear(String animalsYear) {
        this.animalsYear = animalsYear;
    }

    public String getLeapMonth() {
        return leapMonth;
    }

    public void setLeapMonth(String leapMonth) {
        this.leapMonth = leapMonth;
    }

    public String getCyclical() {
        return cyclical;
    }

    public void setCyclical(String cyclical) {
        this.cyclical = cyclical;
    }

    /**
     * @param position 设置点击的日期的颜色位置
     */
    public void setColorDataPosition(int position) {
        if (position >= dayOfWeek && position < daysOfMonth + dayOfWeek) {
            colorDataPosition = position;
            notifyDataSetChanged();
        }

    }

}
